package com.panfeng.xcloud.component.trace.selector;

import brave.servlet.TracingFilter;
import brave.spring.webmvc.TracingHandlerInterceptor;
import com.panfeng.xcloud.component.trace.annotation.EnableTrace;
import com.panfeng.xcloud.component.trace.config.ZipkinTraceAutoConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

/**
 * trace组件选择注入
 *
 * @author xiaobo
 * @version 1.0
 * @since 2018-08-21
 */
@Slf4j
public class TraceImportSelector implements ImportSelector {

	@Override
	public String[] selectImports(AnnotationMetadata importingClassMetadata) {
		Class<?> enableTraceClazz = EnableTrace.class;
		AnnotationAttributes annotationAttributes = AnnotationAttributes
				.fromMap(importingClassMetadata.getAnnotationAttributes(
						enableTraceClazz.getName(), false));
		if(null == annotationAttributes){
			log.info(">>> 未添加EnableTrace注解启动Trace组件 <<<");
			log.info(">>> 未自动配置初始化Trace组件 <<<");
			return new String[] {};
		}
		boolean isEnabled = annotationAttributes.getBoolean("isEnabled");
		if (isEnabled) {
			log.info(">>> 已经成功自动配置初始化Trace组件 <<<");
			return new String[] { TracingHandlerInterceptor.class.getName() ,TracingFilter.class.getName() , ZipkinTraceAutoConfig.class.getName()};
		} else {
			log.info(">>> 未自动配置初始化Trace组件 <<<");
			return new String[] {};
		}
	}
}
